/*
 * @lc app=leetcode.cn id=560 lang=golang
 *
 * [560] 和为 K 的子数组
 */
package Solutions

// @lc code=start
func subarraySum(nums []int, k int) int {
	var mp = make(map[int]int, len(nums))
	mp[0] = 1
	sum := 0
	var ans = 0
	for _, v := range nums {
		sum += v
		if count, ok := mp[sum-k]; ok {
			ans += count
		}
		mp[sum]++
	}
	return ans
}

// @lc code=end
